/**
 * 
 */
package com.syncshot.androidapp.activities;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.syncshot.androidapp.Constant;
import com.syncshot.androidapp.R;

/**
 * @author Stan
 * 
 */
public class InitActivity extends AbstractActivity {
    /** Used locally to tag Logs */
    @SuppressWarnings("unused")
    private static final String TAG               = "InitActivity";

    private EditText            mEditTextUsername = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences settings = getSharedPreferences(Constant.PREF, 0);
        if (settings.getString(Constant.PREF_USERNAME, null) != null) {
            goToHome();
            return;
        }

        setContentView(R.layout.activity_init);
    }
    
    private void goToHome() {
        startActivity(new Intent(this, HomeActivity.class));
        finish();
    }

    public void onClickOK(View view) {
        if (mEditTextUsername == null) {
            mEditTextUsername = (EditText) findViewById(R.id.username_edittext);
        }
        Editable editable = mEditTextUsername.getText();
        if (editable.length() == 0) {
            Toast.makeText(this, R.string.error_username_short, Toast.LENGTH_LONG).show();
            return;
        }

        SharedPreferences settings = getSharedPreferences(Constant.PREF, 0);
        settings.edit().putString(Constant.PREF_USERNAME, editable.toString()).commit();
        goToHome();
        
    }
}
